package com.vst.serverdatalib.entity;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;

import com.vst.serverdatalib.FileUtil;
import com.vst.serverdatalib.SharedPreHelper;
import com.vst.serverdatalib.https.LogUtil;

public class ServerConfigEntity {

    public static final String KEY_VERSION_MAJOR = "version_major";

    public static final String KEY_VERSION_CONF = "version_conf";

    public static final String KEY_SERVER_UPDATE = "server_update";

    public static final String KEY_SERVER_LIVE = "server_live";

    public static final String KEY_SERVER_LIVE_CONFIG = "server_live_play";

    public static final String KEY_SERVER_VOD = "server_vod";

    public static final String KEY_SERVER_VOD_CONFIG = "server_vod_play";

    public static final String KEY_SERVER_IMG = "server_img";

    public static final String KEY_SERVER_MY = "server_my";
    
    public static final String KEY_SERVER_MY_LIVE = "server_mylive";
    
    public static final String KEY_SERVER_GLUE = "server_glue";
    
    public static final String KEY_SERVER_BBS = "server_bbs";

    public static final String KEY_DEFUALT = "default_param";

    public static final String KEY_HOME_PAGE = "api_top_json";

    public static final String KEY_SERVER1 = "server_conf_ext_1";

    public static final String KEY_SERVER2 = "server_conf_ext_2";

    public static final String KEY_SERVER3 = "server_conf_ext_3";

    public static final String KEY_SERVER4 = "server_conf_ext_4";

    public static final String KEY_SERVER5 = "server_conf_ext_5";

    public static final String KEY_HOT_CHANNELS = "api_live_top";

    public static final String KEY_LIVE_DATE_VERSION = "api_live_tvlist_ver";

    public static final String KEY_LIVE_SUBSCRIBE = "api_live_subscribe";

    public static final String KEY_LIVE_BACK_V2 = "api_live_tvback_v2";

    public static final String KEY_LIVE_BACK_V1 = "api_live_tvback_v1";

    public static final String KEY_FEEDBACK = "feedback";

    public static final String KEY_HONOR = "api_user_panel";
    
    private int mVersion_major;

    private int mVersion_conf;
    
    private String serverUpdate;
    
    private String serverLive;
    
    private String serverLiveConfig;
    
    private String serverVod;
    
    private String serverVodConfig;
    
    private String serverImg;
    
    private String serverMy;
    
    private String serverMyLive;
    
    private String serverBBS;
    
    private String serverGlue;
    
    private HashMap<String, UrlRequestEntity> mMapUrlRequestEntitys;

    private static ServerConfigEntity sInstance;
    
    private static ArrayList<String> keys = new ArrayList<String>();
    
    static {
        keys.add(KEY_VERSION_MAJOR);
        keys.add(KEY_VERSION_CONF);
        keys.add(KEY_SERVER_UPDATE);
        keys.add(KEY_SERVER_LIVE);
        keys.add(KEY_SERVER_LIVE_CONFIG);
        keys.add(KEY_SERVER_VOD);
        keys.add(KEY_SERVER_VOD_CONFIG);
        keys.add(KEY_SERVER_IMG);
        keys.add(KEY_SERVER_MY);
        keys.add(KEY_SERVER_MY_LIVE);
        keys.add(KEY_SERVER_GLUE);
        keys.add(KEY_SERVER_BBS);
        keys.add("msg");
        keys.add("retCode");
    }

    private ServerConfigEntity(Context context) {
        InputStream is = null;
        LogUtil.i("ServerConfigEntity");
        try {
            String json = SharedPreHelper.getSerVerConfigContent(context);
            LogUtil.i("SharedPreHelper json = "+json);
            if (TextUtils.isEmpty(json)) {
              is = context.getApplicationContext().getResources().getAssets().open("server_config.html");
              LogUtil.i("getResources is = "+is);
              if(is ==null){
                  return;
              }
              byte[] buf = new byte[1024];
              int len = -1;
              StringBuffer sb = new StringBuffer();
              while ((len = is.read(buf, 0, buf.length)) != -1) {
                  sb.append(new String(buf, 0, len));
              }
              json = sb.toString();
              LogUtil.i("getResources json = "+json);
              if(TextUtils.isEmpty(json)){
                  return;
              }
            }
            JSONObject jsonObject = new JSONObject(json);
            mVersion_major = jsonObject.optInt(KEY_VERSION_MAJOR);
            mVersion_conf = jsonObject.optInt(KEY_VERSION_CONF);
            serverUpdate = jsonObject.optString(KEY_SERVER_UPDATE);
            serverLive= jsonObject.optString(KEY_SERVER_LIVE);
            serverLiveConfig= jsonObject.optString(KEY_SERVER_LIVE_CONFIG);
            serverVod= jsonObject.optString(KEY_SERVER_VOD);
            serverVodConfig= jsonObject.optString(KEY_SERVER_VOD_CONFIG);
            serverImg= jsonObject.optString(KEY_SERVER_IMG);
            serverMy= jsonObject.optString(KEY_SERVER_MY);
            serverMyLive= jsonObject.optString(KEY_SERVER_MY_LIVE);
            serverBBS= jsonObject.optString(KEY_SERVER_BBS);
            serverGlue= jsonObject.optString(KEY_SERVER_GLUE);
            mMapUrlRequestEntitys = new HashMap<String, UrlRequestEntity>();
            @SuppressWarnings("unchecked")
            Iterator<String> it = jsonObject.keys();
            while (it.hasNext()) {
                String key = it.next();
//                LogUtil.d("key=" + key);
                if(!keys.contains(key)){
                    mMapUrlRequestEntitys.put(key, new UrlRequestEntity((JSONObject) jsonObject.opt(key)));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            FileUtil.closeIo(is);
        }
    }

    public static ServerConfigEntity getInstance(Context context) {
        if (sInstance == null) {
            synchronized (ServerConfigEntity.class) {
                if (sInstance == null) {
                    sInstance = new ServerConfigEntity(context);
                }
            }
        }
        return sInstance;
    }

    public int getVersion_major() {
        return mVersion_major;
    }

    public int getVersion_conf() {
        return mVersion_conf;
    }

    public HashMap<String, UrlRequestEntity> getMapUrlRequestEntitys() {
        return mMapUrlRequestEntitys;
    }

    public String getServerUpdate() {
        return serverUpdate;
    }

    public String getServerLive() {
        return serverLive;
    }


    public String getServerLiveConfig() {
        return serverLiveConfig;
    }


    public String getServerVod() {
        return serverVod;
    }

    public String getServerVodConfig() {
        return serverVodConfig;
    }

    public String getServerImg() {
        return serverImg;
    }


    public String getServerMy() {
        return serverMy;
    }


    public static void setsInstance(ServerConfigEntity sInstance) {
        ServerConfigEntity.sInstance = sInstance;
    }

    public String getServerMyLive() {
        return serverMyLive;
    }

    public String getServerGlue() {
        return serverGlue;
    }

    public String getServerBBS() {
        return serverBBS;
    }
   
 
}
